package com.FH.permission.sys_role_member;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Stateless
public class SysRoleMemberDAOImpl extends AccessDB implements SysRoleMemberDAO {

    @Override
    public ResultDTO addSysRoleMember(SysRoleMemberPullDTO sysRoleMemberPullDTO) {

        ResultDTO resultDTO = new ResultDTO();

        int resultls = 0;

        if(sysRoleMemberPullDTO.getSys_role_id().length > 0){

            String deleteSql = "delete from sys_role_member where member_id = '" + sysRoleMemberPullDTO.getMember_id() + "' ";

            addReportingTo(deleteSql);  //给用户添加或更新时删除所有用户角色

            for(int i =0;i<sysRoleMemberPullDTO.getSys_role_id().length;i++){

                if(null!=sysRoleMemberPullDTO.getSys_role_id()[i]&&!"".equals(sysRoleMemberPullDTO.getSys_role_id()[i])){

                    String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

                    String sql = "insert into sys_role_member (sys_role_member_id,sys_role_id,member_id) values " +
                            "('" + uuid + "'," + Integer.parseInt(sysRoleMemberPullDTO.getSys_role_id()[i]) + ",'" + sysRoleMemberPullDTO.getMember_id() + "')";
                    resultls = this.addReportingTo(sql);
                }
            }
        }

//        resultls = this.addReportingTo(sql);


        if (resultls != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion("角色分配成功");
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion("角色分配失败");
        }

        return resultDTO;
    }

    @Override
    public ResultDTO getUserRoleById(String member_id) {

        ResultDTO resultDTO = new ResultDTO();

        String sql = "SELECT DISTINCT sys_role_id FROM sys_role_member WHERE member_id = '" + member_id + "' ";

        List<String[]> results = new ArrayList<String[]>();

        List<SysRoleShowDTO> roleList = new ArrayList<SysRoleShowDTO>();

        results = this.getReportingTo(sql);

        for(int i=0;i<results.size();i++){
            SysRoleShowDTO sysRoleShowDTO = new SysRoleShowDTO();
            sysRoleShowDTO.setSys_role_id(results.get(i)[1]);
//            sysRoleShowDTO.setSys_role_name(results.get(i)[2]);

            roleList.add(sysRoleShowDTO);
        }

        if (results.size() > 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion("角色获取成功");
            resultDTO.setValues(roleList);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion("角色获取失败");
        }

        return resultDTO;
    }

    /**
     * 同步基础平台用户角色
     *
     * @param str
     */
    @Override
    public ResultDTO updateSysRoleMember(String str) {

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject jsonObject = new JSONObject(str);

            String sql = "SELECT member_id FROM kam_member WHERE userId = '"+jsonObject.getString("userId")+"'";

            String member_id = getReportingToOne(sql);
            String[] roleIds = jsonObject.getString("roleIds").substring(1,jsonObject.getString("roleIds").length()-1).split(",");

            SysRoleMemberPullDTO sysRoleMemberPullDTO = new SysRoleMemberPullDTO();

            sysRoleMemberPullDTO.setMember_id(member_id);
            sysRoleMemberPullDTO.setSys_role_id(roleIds);

            resultDTO = addSysRoleMember(sysRoleMemberPullDTO);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        return resultDTO;
    }
}
